package ro.upt.pcbe;

import javax.jms.*;

public class Reader {

    private String name;
    private NewsListener listener;
    private Connection connection;
    private String authorName;
    private Session session = null;
    private MessageConsumer consumer = null;
    private MessageProducer producer = null;
    private Topic confirmationTopic;

    public Reader(String name, Connection connection,
            String newsAuthorName, Topic confirmTopic) throws JMSException {
        this.name = name;
        this.connection = connection;
        this.authorName = newsAuthorName;


        this.confirmationTopic = confirmTopic;
    }

    public void subscribeToTopic(Topic topic) throws JMSException {
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        if (this.authorName != null) {
            consumer = session.createConsumer(topic, this.createSelector());
        } else {
            consumer = session.createConsumer(topic);
        }
        listener = new NewsListener(this.getName(), confirmationTopic, connection);
        consumer.setMessageListener(listener);

    }

    private String createSelector() {
        return Constants.EDITOR + " = '" + this.authorName + "'";
    }

    public String getName() {
        return name;
    }

    public String getAuthorName() {
        return authorName;
    }
}
